package com.kfd.demo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.kfd.demo.entity.Reqs;
import com.kfd.demo.entity.user.User;
import com.kfd.demo.service.UserServe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@CrossOrigin(origins = "*")
public class UserController {

    @Autowired
    UserServe userServe;

    @ResponseBody
    @PostMapping(value = "/loginTo", produces = "application/json;charset=UTF-8")
    public Reqs loginTo(User theuser) throws JsonProcessingException {
        System.out.println("访问了登录" + theuser);
//        传入该用户数据，登录操作
        List u = userServe.login(theuser);
        if (u == null) {
            u = userServe.login(theuser);
        }
        System.out.println(u + "这里是usercon拿到的数据");
        return new Reqs(200, "",u);
    }

//    用户下单
@ResponseBody
@PutMapping(value = "/userPurchase", produces = "application/json;charset=UTF-8")
public Reqs UserPurchase(Integer uid, int goodsID, int groupId,  int number,String cartNote){
        int success = userServe.UserPurchase(uid, goodsID,groupId,number,cartNote);
        return new Reqs(200,"下单成功，前端请跳转到订单详情并查询指定订单号的订单",success);
    }

    @ResponseBody
    @PostMapping(value = "/userOrder", produces = "application/json;charset=UTF-8")
    public Reqs UserOrder(Integer uid,Integer groupId){
        return new Reqs(200,"用户订单，注意订单分组",userServe.getUserOrder(uid,groupId));
    }

}
